package com.wr.controller;


import com.wr.model.RestCode;
import com.wr.model.RestResponse;
import com.wr.model.User;
import com.wr.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author weirong
 * @Title:
 * @Description:
 * @date 2018/6/511:43
 */
@RestController
@RequestMapping("user")
public class UserController {

    Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private UserService userService;


    /**
     * @desc 注册添加用户
     * @param user
     * @return
     */
    @RequestMapping("/add")
    public RestResponse<User> add(@RequestBody User user) {

        Integer id = userService.insertSelective(user);
        user.setId(id);
        return RestResponse.success(user);
    }

    @RequestMapping("getUserByEmail")
    public RestResponse<User> getUserByEmail(@RequestParam String email) {
        User user = userService.getByEmail(email);
        return RestResponse.success(user);
    }

    @RequestMapping("getUserByUserName")
    public RestResponse<User> getUserByUserName(@RequestParam String username) {
        User user = userService.getUserByUserName(username);
        return RestResponse.success(user);
    }

    @RequestMapping("getUserById")
    public RestResponse<User> getUserById(@RequestParam Integer id) {
        User user = userService.selectByPrimaryKey(id);
        return RestResponse.success(user);
    }

    @RequestMapping("authLogin")
    public RestResponse<User> authLogin(@RequestBody User user) {
        User u = null;
        try {
            u = userService.authLogin(user);
            return RestResponse.success(u);
        } catch (Exception e) {
            return RestResponse.error(RestCode.UNKNOWN_ERROR);
        }
    }

    @RequestMapping("getUserByToken")
    public RestResponse<User> getUserByToken(@RequestParam String token){

        User u = null;
        try {
            u = userService.getLoginedUserByToken(token);
            if(u != null) {
                return RestResponse.success(u);
            }
        } catch (Exception e) {
            return RestResponse.error(RestCode.TOKEN_INVALID);
        }
        return RestResponse.error(RestCode.UNKNOWN_ERROR);
    }

    @RequestMapping("logout")
    public RestResponse<User> logout(@RequestParam String token) {
        userService.logout(token);
        return RestResponse.success();
    }

    @RequestMapping("resetPw")
    public RestResponse<User> resetPw(@RequestBody User user) {
        try {
            userService.resetPw(user);
            return RestResponse.success();

        } catch (Exception e) {
            logger.error(e,e);
            return RestResponse.error(RestCode.UNKNOWN_ERROR);
        }

    }
    @RequestMapping("update")
    public RestResponse<User> update(@RequestBody User user) {
        try {
            userService.updateByPrimaryKeySelective(user);
            return RestResponse.success();

        } catch (Exception e) {
            logger.error(e,e);
            return RestResponse.error(RestCode.UNKNOWN_ERROR);
        }

    }
}
